package com.kyh.controller.web.sys;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kyh.properties.AppProps;
import com.kyh.sys.entity.Admin;
import com.kyh.sys.entity.Menu;
import com.kyh.sys.entity.Module;
import com.kyh.util.ReqUtil;

@Controller
public class WebController {
	private static final Logger logger = Logger.getLogger(WebController.class);

	@GetMapping({ "", "/", "/index" })
	public String index(
			Model model,
    		HttpServletRequest request,
    		Long moduleId
    		) {
		
		// 项目名称
		model.addAttribute("projectName", AppProps.get("projectName"));
		
		if (moduleId == null) {
			moduleId = 2L;
		}
		
		// 模块名称
    	Module module = new Module().setModuleId(moduleId).queryFirst();
		model.addAttribute("moduleName", module.getModuleName());
		
    	Long adminid = ReqUtil.getAdminId(request);
    	Admin admin = new Admin().setAdminid(adminid).queryFirst();
    	
    	if(null == admin){
    		
			return "login";
		}
    	
    	// 用户名
    	model.addAttribute("adminLoginName", admin.getAdminloginname());
    	Long roleId = admin.getRoleId();
    	if (roleId.longValue() == 1) {
    		roleId = null;
		}

    	Module condModule = new Module();
		if (roleId != null) {
			condModule
			.where("A.module_id in (select module_id from sys_menu where menu_id in (select menu_id from sys_role_menu where role_id = '" + roleId.longValue() + "' and status = 1))");
		}
		List<Module> listModule = new Module().queryList();
		model.addAttribute("listModule", listModule);
		
		JSONArray JAMenuLev1 = new JSONArray();

		Menu condMenu = new Menu().setMenuPid(0L)
				.setModuleId(moduleId)
				.setMenuLev(1)
				.setOrderby("A.menu_sort_no asc");
		if (roleId != null) {
			condMenu.where("A.menu_id in (select menu_id from sys_role_menu where role_id = " + roleId + " and status = 1)");
		}
		List<Menu> listMenuLev1 = condMenu.queryList();
		
		for (Menu menu1 : listMenuLev1) {
			
			JSONObject JOMenuLev1 = menu1.modelTable();


			Menu condMenu2 = new Menu().setMenuPid(menu1.getMenuId()).setOrderby("A.menu_sort_no asc");
			if (roleId != null) {
				condMenu2.where("A.menu_id in (select menu_id from sys_role_menu where role_id = " + roleId + " and status = 1)");
			}
			List<Menu> listMenuLev2 = condMenu2.queryList();
			
			JSONArray JAMenuLev2 = new JSONArray();
			for (Menu menu2 : listMenuLev2) {
				JAMenuLev2.add(menu2.modelTable());
			}
			JOMenuLev1.put("JAMenuLev2", JAMenuLev2);
			
			JAMenuLev1.add(JOMenuLev1);
		}
		model.addAttribute("JAMenuLev1", JAMenuLev1);
		
		return "index";
	}

    @GetMapping("/login")
	public String login(Model model, HttpServletRequest request, HttpServletResponse response) {
		logger.info("管理后台登录页面");
		
		// 项目名称
		model.addAttribute("projectName", AppProps.get("projectName"));
		
		return "login";
	}
	
	@GetMapping("/logout")
	public void logout(
			Model model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// 项目名称
		model.addAttribute("projectName", AppProps.get("projectName"));
		
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(ReqUtil.ADMIN_TOKEN)) {
					cookie.setMaxAge(0);// 立即销毁cookie
					response.addCookie(cookie);
					break;
				}
			}
		}
		response.sendRedirect("/login"); // 未登录，跳转到登录页
	}
}
